package junior.db.dao.update;

import lombok.Getter;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @author wubo
 * @date 2025/3/10 15:02
 */
@Getter
class VersionWrapper {
    private String name;
    private Field field;
    private Method getter;
    
    public VersionWrapper(String name, Field field) {
        this.name = name;
        this.field = field;
    }
    
    public VersionWrapper(String name, Method getter) {
        this.name = name;
        this.getter = getter;
    }
    
    public Object getValue(Object entity) {
        try {
            if (field != null) {
                field.setAccessible(true);
                return field.get(entity);
            }
            if (getter != null) {
                return getter.invoke(entity);
            }
        } catch (Exception e) {
            // ignore
        }
        return null;
    }
}
